package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class XiluNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;

		String[] regex={ 
							"http://.*.xilu.com/[0-9]{8}/[0-9]*.html",
							"http://club.xilu.com/.*/.*-[0-9]*-[0-9]*.html",
							"http://.*.xilu.com/[0-9]{8}/news_[0-9]+_[0-9]+[_0-9]*.html",
							"http://.*.xilu.com/[0-9]{4}/[0-9]{4}/news_[0-9]+_[0-9]+[_0-9]*.html",
							"http://.*.xilu.com/.*[0-9]{4}/[0-9]{4}/[0-9]+[_0-9]*.html",
							};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String html=urlMeta.getHtml();
		Document doc=Jsoup.parse(html);
		if (html == null) {
		}
		String title="";
		String content="";
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		String author=null;
		NewsMeta meta=new NewsMeta();
		if (doc.select("form[name=login]").size()>0) {
			meta.setUpdateUrl("forbid");
			return meta;
		}
		
		meta.setUrl(url);
		meta.setType(0);
		
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("h1").text();
		if (StringUtils.isBlank(title)) {
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
		}
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.newsinfo_con,div.newsinfo").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("dd.yellowD").text());
		}
		/*
		 * 解析内容
		 */
		doc.select("ul.xladAdvList").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#pagetext"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Meat.black16"));
		}
		Elements select=doc.select("#pagetext p:containsOwn(推荐阅读：)");
		if(select!=null && select.size()>0){
			content=StringUtils.substringBefore(content, "推荐阅读：");
		}
		if (StringUtils.isBlank(title)) {
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setAuthor(author);
		meta.setCommentNum(commentNum);

		return meta;
	}


	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url="http://shizheng.xilu.com/20140519/1000150001969860.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		XiluNewsAnalyse a=new XiluNewsAnalyse();
		boolean detailPage=a.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("regex not match");
		}

	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
